﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta><style>/*<![CDATA[*/

table{border: 1px solid gray;}
td{border: 1px dotted gray;}
p{margin: 3px 0 3px 0; padding: 0;}
#ID_Footer{font-size: small; font-style: italic; text-align: right; margin-top: 4em; padding-top: 4px; border-top: 2px solid gray;}

/*]]>*/</style><title>第6章 面向对象PHP</title></head><body>
<div><span style="font-family: 微软雅黑; font-size: 18pt; font-weight: bold; padding-left: 49pt; line-height: 190%">第</span><span style="font-family: Consolas; font-size: 18pt; font-weight: bold; padding-left: 49pt; line-height: 190%">6</span><span style="font-family: 微软雅黑; font-size: 18pt; font-weight: bold; padding-left: 49pt; line-height: 190%">章 &nbsp; &nbsp; &nbsp; &nbsp;面向对象的</span><span style="font-family: Consolas; font-size: 18pt; font-weight: bold; padding-left: 49pt; line-height: 190%">PHP</span></div>
<div><span style="font-family: 微软雅黑; font-size: 14pt; padding-left: 70pt; line-height: 170%">6.1 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 14pt; padding-left: 70pt; line-height: 170%">在</span><span style="font-family: Calibri; font-size: 14pt; padding-left: 70pt; line-height: 170%">PHP</span><span style="font-family: 宋体; font-size: 14pt; padding-left: 70pt; line-height: 170%">中创建类、属性和操作</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; color: #000000; line-height: 160%">6.1.1 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; color: #000000; line-height: 160%">类的结构</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">创建</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">类</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，关键字</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">: </span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">class</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">类中声明变量，即创建</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">属性</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，作用域相关关键字：</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">public</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">, </span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">private</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">, </span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">protected</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">类的</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 240%">操作</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">，即类定义中声明函数。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">6.1.2 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">构造函数</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">名称必须为：</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 190%">__construct()</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 190%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 190%">创建一个对象时调用构造函数，可以手工调用，可带参数</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 240%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 240%">PHP</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">支持函数重载，可以提供多个具有相同名称及不同数量或类型参数的函数。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #000000; line-height: 160%">6.1.3 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">析构函数</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">在</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">销毁</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">一个类之前执行一些操作或完成一些功能</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">通常在所有对该类的引用都被重置或超出作用域范围时</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">自动发生</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">。</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">名称必须为：</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">__destruct()</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 240%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">不能带有任何参数</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 14pt; font-weight: bold; padding-left: 70pt; line-height: 170%">6.2 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 18pt; font-weight: bold; padding-left: 70pt; line-height: 170%">类的实例化</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; color: #ff0000; line-height: 240%">实例化</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">：类声明后，创建一个对象，并使用这个对象</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">关键词：</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">new</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">$a = </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">new</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%"> classname(&quot;First&quot;);</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 240%">$c = </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; padding-left: 21pt; line-height: 240%">new</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 240%"> classname();</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 14pt; font-weight: bold; padding-left: 70pt; line-height: 170%">6.3 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 18pt; font-weight: bold; padding-left: 70pt; line-height: 170%">使用类的属性</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">类中的特殊指针：</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">$this</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 190%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 190%">若类中属性为</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 190%">$attribute</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 190%">，访问该变量可使用</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 40pt; line-height: 190%">$this-&gt;attribute</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">通过使用</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 190%">__get()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">和</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 190%">__set()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">函数来实现对属性的访问，即</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 190%">访问函数</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">__get()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">函数，带一个参数（属性的名称），返回该属性的值</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt">function __get($name){</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt"> &nbsp; &nbsp;return $this-&gt;name;</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 21pt">}</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">__set()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">函数，两个参数，属性的名称和要被设置的值</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt">function __set($name, $value){</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt">$this-&gt;name = $value;</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 21pt">}</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">并不会直接访问这些函数，通常函数名称前带有双下划线（</span><span style="font-family: Consolas; font-size: 12pt; line-height: 190%">__</span><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">）表明在</span><span style="font-family: Consolas; font-size: 12pt; line-height: 190%">PHP</span><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">中这些函数具有特殊意义。</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">间接调用</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; line-height: 170%">__set()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">函数例子：</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt; line-height: 190%">$a-&gt;attribute = 5;</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">间接调用</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; line-height: 170%">__get()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">函数例子：</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt; line-height: 240%">$a-&gt;attribute;</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 14pt; font-weight: bold; padding-left: 70pt; line-height: 170%">6.4 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 18pt; font-weight: bold; padding-left: 70pt; line-height: 170%">使用</span><span style="font-family: Calibri; font-size: 18pt; font-weight: bold; padding-left: 70pt; line-height: 170%">private</span><span style="font-family: 宋体; font-size: 18pt; font-weight: bold; padding-left: 70pt; line-height: 170%">和</span><span style="font-family: Calibri; font-size: 18pt; font-weight: bold; padding-left: 70pt; line-height: 170%">public</span><span style="font-family: 宋体; font-size: 18pt; font-weight: bold; padding-left: 70pt; line-height: 170%">关键字控制访问</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">访问修饰符</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">：控制</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">属性</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">和</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">方法</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">的可见性，放置在属性和方法声明之前。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">public</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">:</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">默认值，可在内部和外部进行访问</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">private</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">: </span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">只能在类的内部进行访问，不会被继承</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">protected</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">: </span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">只能在类的内部进行访问，可以被继承</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 14pt; font-weight: bold; padding-left: 70pt; line-height: 170%">6.5 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 18pt; font-weight: bold; padding-left: 70pt; line-height: 170%">在</span><span style="font-family: Calibri; font-size: 18pt; font-weight: bold; padding-left: 70pt; line-height: 170%">PHP</span><span style="font-family: 宋体; font-size: 18pt; font-weight: bold; padding-left: 70pt; line-height: 170%">中实现继承</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">关键词</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">&quot;</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">extends</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">&quot;</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt; line-height: 240%">class B </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #2f5496; padding-left: 21pt; line-height: 240%">extends</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt; line-height: 240%"> A</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #000000; line-height: 160%">6.5.1 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">通过继承使用</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">private</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">和</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">protected</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">访问修饰符控制可见性</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; color: #ff0000; line-height: 170%">private</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">：修饰的属性或方法，不能被继承</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">protected</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">：修饰的属性或方法，在类的外部不可见，但可被继承，且只能在子类内部使用。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">6.5.2 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">重载</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; color: #ff0000; line-height: 170%">重载</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">：某个属性在子类中赋予一个与其超类属性不同的默认值，或者操作赋予一个与其超类操作不同的功能。</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">在子类中重载属性或操作并</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">不会影响超类</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">。</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">如果不使用替代，一个子类将继承超类的所有属性和操作，如果子类提供了替代定义，该</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">替代定义将有优先极</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，并且重载初始定义。（</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">p128</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">）</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">parent</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">关键字允许调用父类操作的最初版本。</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 160%">e.g., parent::operation();</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">虽然调用了父类的最被版本，但属性值仍使用当前类的属性值。</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">可以</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">多层继承</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">（即多层，</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">B</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">继承</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">A</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">，</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">C</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">再继承</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">B</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">）</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">6.5.3 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">使用关键字</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">final</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">禁止继承和重载</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; color: #ff0000; line-height: 170%">final</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">关键字，在函数声明前使用这个关键字，则此函数将</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">不能在任何子类中被重载</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">final function operation() {…}</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">final</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">关键字还可以用来，</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">禁止一个类被继承</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">final class A{</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 21pt; line-height: 240%">}</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">6.5.4 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">理解多重继承</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">PHP</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">不支持多重继承</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，即每个类都只能继承一个父类。</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">但一个父类可以有多少个子类没有限制。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">6.5.5 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">实现接口</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">若需要实现</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">多重继承功能</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，可以通过</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">接口</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">。</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">接口</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">：指定一个实现了该接口的类必须实现的一系列函数。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #ff0000; padding-left: 21pt">interface</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #ff0000; padding-left: 21pt"> </span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt">Displayable</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 21pt">{</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt"> &nbsp; &nbsp;function display();</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 21pt">}</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ed7d31; padding-left: 21pt">class</span><span style="font-family: Consolas; font-size: 11pt; color: #ed7d31; padding-left: 21pt"> </span><span style="font-family: Consolas; font-size: 11pt; color: #2f5496; padding-left: 21pt">webPage </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 21pt">implements</span><span style="font-family: Consolas; font-size: 11pt; color: #ff0000; padding-left: 21pt"> </span><span style="font-family: Consolas; font-size: 11pt; color: #2f5496; padding-left: 21pt">Displayable</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 21pt">{</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt"> &nbsp; &nbsp;function display()</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 21pt">{</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 21pt">}</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 21pt">}</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">类可以继承一个类，同时又可以实现</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">一个或多个</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">接口。</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">如果没有实现接口中指定的方法，将会产生一个致命错误。</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 14pt; font-weight: bold; padding-left: 70pt; line-height: 170%">6.6 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 18pt; font-weight: bold; padding-left: 70pt; line-height: 170%">编写类代码</span><span style="font-family: Calibri; font-size: 18pt; font-weight: bold; padding-left: 70pt; line-height: 170%">(p130)</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">需要显示没有经过</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">PHP</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">处理的静态</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">HTML</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">时，可以使用</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">PHP</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">结束标记</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">(?&gt;)</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">、输入</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">HTML</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，然后再函数体内部使用一个</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">PHP</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">打开标记</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">(&lt;?php).</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">判断按钮</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">URL</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">是否指向当前页：</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">strpos(</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #ff0000; padding-left: 21pt; line-height: 160%">$__SERVER[&apos;PHP_SELF&apos;]</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">, $url) == false</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">count</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">($ary),</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">数组中元素的个数</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 14pt; font-weight: bold; padding-left: 70pt; line-height: 170%">6.7 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 18pt; font-weight: bold; padding-left: 70pt; line-height: 170%">理解</span><span style="font-family: Calibri; font-size: 18pt; font-weight: bold; padding-left: 70pt; line-height: 170%">PHP</span><span style="font-family: 宋体; font-size: 18pt; font-weight: bold; padding-left: 70pt; line-height: 170%">面向对象的高级功能</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; color: #000000; line-height: 160%">6.7.1 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; color: #000000; line-height: 160%">使用</span><span style="font-family: Consolas; font-size: 12pt; color: #000000; line-height: 160%">Per-Class</span><span style="font-family: 宋体; font-size: 12pt; color: #000000; line-height: 160%">常量</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">Perclass</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">常量，在不需要初始化类的情况下使用类中定义的常量。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">class Math</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 21pt; line-height: 160%">{</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 21pt; line-height: 160%">const</span><span style="font-family: Consolas; font-size: 11pt; color: #ff0000; padding-left: 21pt; line-height: 160%"> </span><span style="font-family: Consolas; font-size: 11pt; color: #c45911; padding-left: 21pt; line-height: 160%">pi = 3.14159;</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%">}</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 240%">echo Math</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 21pt; line-height: 240%">::</span><span style="font-family: Consolas; font-size: 11pt; color: #c45911; padding-left: 21pt; line-height: 240%">pi;</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">6.7.2 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">实现静态方法</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">使用</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">static</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">关键字，适用于允许在未初始化类的情况下就可以调用的方法。</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 240%">静态方法</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">中，</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 240%">不能使用</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; line-height: 240%">$this</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">关键字。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">6.7.3 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">检查类的类型和类型提示</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; color: #ff0000; line-height: 170%">instanceof</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">关键字，允许检查一个对象的类型。</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">检查一个对象是否是特定类的实例。</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">是否是从某个</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 160%">类</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">继承过来</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 190%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 190%">是否实现了某个</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 190%">接口</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">类型提示</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">：向函数传递参数时，使用类类型提示。</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">可以指定必须传入的参数的类型，如果传入的不是指定的类型，将产生一个错误。</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 240%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">类型检查等价于</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 240%">instanceof</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">的作用。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #000000; line-height: 160%">6.7.4 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">延迟静态绑定</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; color: #ff0000; line-height: 170%">延迟静态绑定</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">（</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">late static binding</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">）</span><span style="font-family: Times New Roman; font-size: 12pt; line-height: 170%">,</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">允许在一个静态继承的上下文中对一个被调用类的引用。</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">父类可以使用</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">子类重载的静态方法</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">__CLASS__</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">:</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">类名称</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">6.7.5 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">克隆对象</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; color: #ff0000; line-height: 170%">clone</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">关键字，允许复制一个已有的对象</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">$c = clone $b;</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 190%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 190%">创建一个与对象</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 190%">$b</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 190%">具有相同类的副本，并且具有相同的属性值。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">__clone()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">方法，当以上例所式方式使用</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">clone</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">关键字时，该方法自动被调用。</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">(p140)</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">可以改变克隆过来的默认行为</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">在</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 160%">__clone()</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">方法中定义所需要的确切复制行为</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 240%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">在默认行为</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 240%">创建一个副本后能够被调用</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">，可以只改变希望改变的内容。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #000000; line-height: 160%">6.7.6 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">使用抽象类</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; color: #ff0000; line-height: 170%">抽象类</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">类不能被实例化</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 140%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 140%">只提供类方法的声明，没有具体实现（即</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 140%">抽象方法</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 140%">）</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #ff0000; padding-left: 21pt; line-height: 190%">abstract</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #ff0000; padding-left: 21pt; line-height: 190%"> </span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 190%">operationX($param1, $param2);</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">包含抽象方法的任何抽象类本身必须是抽象的</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">abstract</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%"> class A{</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; padding-left: 21pt; line-height: 160%">abstract</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> function operationX($param1, $param2);</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 21pt; line-height: 190%">}</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">作用：在层次关系中，</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 240%">确保每一个子类都包含并重载了某些特定的方法</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">，这也可以通过接口来实现。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">6.7.7 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">使用</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">__call()</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">重载方法</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">php</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">方法的重载，利用</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; line-height: 240%">__call()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">方法来实现。</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">两个参数，</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">$method</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">为被调用的方法的名称，第二个参数，传递给该方法的参数数组。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">public function __call($method, $p)</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%">{</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp;if($method == &quot;display&quot;){</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp;if (is_object($p[0])){</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$this-&gt;displayObject($p[0]);</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp;} else if (is_array($p[0])){</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$this-&gt;displayArray($p[0]);</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp;} else {</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$this-&gt;displayScalar($p[0]);</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp;}</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp;}</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 190%">}</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">重载函数的调用：</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">实例化包含</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 160%">__call()</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">方法的类</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">然后调用</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 160%">__call</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">方法中实现的</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 160%">$method</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">对应的重载方法（如</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 160%">display()</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">）</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 240%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 240%">display()</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">方法不用实现。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #000000; line-height: 160%">6.7.8 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">使用</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">__autoload()</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">方法</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; color: #ff0000; line-height: 170%">__autoload()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，非类方法，是一个单独的函数，可在任何类声明之外声明这个函数。</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">在实例化一个还没有被声明的类时自动调用。</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 190%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 190%">用途：尝试包含或请求任何用来初始化所需类的文件。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">function __autoload($name){</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp;require($name.&quot;.php&quot;); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 21pt; line-height: 160%">包括一个与该类相同名称的文件</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 21pt; line-height: 240%">}</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #000000; line-height: 160%">6.7.9 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">实现迭代器和迭代（</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">p142-144</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">）</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">可使用</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">foreach()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">方法，通过循环方式取出一个对象的所有属性，就像数组方式一样。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">$x = new myClass;</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%">foreach ($x as $attribute) {</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp;echo $attribute. &quot;&lt;br /&gt;&quot;;</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 240%">}</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">通过实现</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">iterator</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">（迭代器）</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">来实现更加复杂的行为。</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">将要迭代的类实现</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 40pt; line-height: 160%">IteratorAggregate</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 40pt; line-height: 160%">接口</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">，并且定义一个能够返回</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 40pt; line-height: 160%">迭代类实例</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">的</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 40pt; line-height: 160%">getIterator</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">方法。</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">该</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 160%">迭代类</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">必须实现</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 40pt; line-height: 160%">Iterator</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 40pt; line-height: 160%">接口</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">，包含一系列必须实现的方法。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 190%">class ObjectIterator </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #ff0000; padding-left: 21pt; line-height: 190%">implements</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #ff0000; padding-left: 21pt; line-height: 190%"> </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #ff0000; padding-left: 21pt; line-height: 190%">Iterator</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 190%"> {</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp;...</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp;function </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 21pt; line-height: 160%">rewind</span><span style="font-family: Consolas; font-size: 11pt; color: #c45911; padding-left: 21pt; line-height: 160%">(){...}</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp;function </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 21pt; line-height: 160%">valid</span><span style="font-family: Consolas; font-size: 11pt; color: #c45911; padding-left: 21pt; line-height: 160%">(){...}</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp;function </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 21pt; line-height: 160%">key</span><span style="font-family: Consolas; font-size: 11pt; color: #c45911; padding-left: 21pt; line-height: 160%">(){...}</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp;functon </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 21pt; line-height: 160%">current</span><span style="font-family: Consolas; font-size: 11pt; color: #c45911; padding-left: 21pt; line-height: 160%">(){...}</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp;function </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 21pt; line-height: 160%">next</span><span style="font-family: Consolas; font-size: 11pt; color: #c45911; padding-left: 21pt; line-height: 160%">(){...}</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp;function </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 21pt; line-height: 160%">value</span><span style="font-family: Consolas; font-size: 11pt; color: #c45911; padding-left: 21pt; line-height: 160%">(){...}</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%">}</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%">class Object </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 21pt; line-height: 160%">implements</span><span style="font-family: Consolas; font-size: 11pt; color: #ff0000; padding-left: 21pt; line-height: 160%"> </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 21pt; line-height: 160%">IteratorAggregate</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 21pt; line-height: 160%">{</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp;...</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp;function </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 21pt; line-height: 160%">getIterator</span><span style="font-family: Consolas; font-size: 11pt; color: #c45911; padding-left: 21pt; line-height: 160%">()</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 21pt; line-height: 160%">{</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp;return new ObjectIterator($this);</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 21pt; line-height: 160%">}</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 21pt; line-height: 240%">}</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #000000; line-height: 160%">6.7.10 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">将类转换成字符串</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">(p144)</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">类定义中实现</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">__toString()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">函数，尝试打印该类时，则会调用此函数。</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; color: #ff0000; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 40pt; line-height: 160%">__toString()</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">函数中的所有返回内容都将被</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 160%">echo</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">语句打印</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">var_export()</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">函数</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">，打印出类中所有属性值。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">6.7.11 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">使用</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">Reflection</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">（反射）</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">API</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; color: #ff0000; line-height: 170%">反射</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">是通过访问已有类和对象来找到类和对象的结构和内容的能力。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">$class = new ReflectionClass(&quot;page&quot;);</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">echo &quot;&lt;pre&gt;&quot;.$class.&quot;&lt;/pre&gt;&quot;;</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">//</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">使用了</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">Reflection</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">类的</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">__toStirng()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">方法来打印</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">Page</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">类的数据。</span></div>
<div><br /></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">===================================</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">Outline</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">=====================================</span></div>
<div><br /></div>
<div>
<ol>
<li><span style="font-family: 微软雅黑; font-size: 12pt">封装， 对象的操作也就是对象的接口(P118)， 多态性</span></li>
<li><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold">class </span><span style="font-family: 微软雅黑; font-size: 12pt">key word, __construct(), __destruct()(无参数）</span></li>
<li><span style="font-family: 微软雅黑; font-size: 12pt">new, $this,</span><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold; color: #ff0000"> __get()</span><span style="font-family: 微软雅黑; font-size: 12pt; color: #000000"> 一个参数, </span><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold; color: #ff0000">__set()</span><span style="font-family: 微软雅黑; font-size: 12pt; color: #000000">两个参数</span></li>
<li><span style="font-family: 微软雅黑; font-size: 12pt">public, private, protected</span></li>
<li><span style="font-family: 微软雅黑; font-size: 12pt">extends, </span><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold; color: #ff0000">parent</span><span style="font-family: 微软雅黑; font-size: 12pt; color: #000000">::operation();</span></li>
<li><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold; color: #ff0000">final</span><span style="font-family: 微软雅黑; font-size: 12pt; color: #ff0000">, </span><span style="font-family: 微软雅黑; font-size: 12pt">类前面，函数前面</span></li>
<li><span style="font-family: 微软雅黑; font-size: 12pt; color: #000000">接口，指定实现了类必须实现的一系列函数：</span><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold; color: #ff0000">interface, implements</span></li></ol>
<div><br /></div>
<div>
<ol>
<li><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold; color: #ff0000">const</span></li>
<li><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold; color: #ff0000">static</span></li>
<li><span style="font-family: 宋体; font-size: 12pt">延迟静态绑定：</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000">__CLASS__</span></li>
<li><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold; color: #ff0000">clone, __clone()</span></li>
<li><span style="font-family: 微软雅黑; font-size: 12pt">抽象类</span></li>
<li><span style="font-family: 微软雅黑; font-size: 12pt; color: #ff0000">__call()</span></li>
<li><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold; color: #ff0000">__autoload()</span></li>
<li><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold; color: #ff0000">iterator, </span><span style="font-family: 微软雅黑; font-size: 11pt; font-weight: bold; color: #ff0000">IteratorAggregate, getIterator， rewind(), valid(), key(), value(), next()</span></li>
<li><span style="font-family: 微软雅黑; font-size: 11pt; font-weight: bold; color: #ff0000">__toString()</span><span style="font-family: 微软雅黑; font-size: 12pt">，</span><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold; color: #ff0000">var_export()</span></li>
<li><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold; color: #ff0000">ReflectionClass</span></li></ol>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div></div></div><script type="text/javascript" language="javascript" src="jquery.js"></script><script type="text/javascript" language="javascript" src="itemlink.js"></script></body></html>